package com.kefu.call.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Maps;
import com.kefu.call.model.CallAvoidDial;
import com.kefu.call.service.CallAvoidDialService;
import com.kefu.call.vo.CallAvoidDialInsertVo;
import com.kefu.call.vo.CallAvoidDialSearchVo;
import com.kefu.common.annotation.Permission;
import com.kefu.common.controller.AbstractBaseController;
import com.kefu.common.result.Result;
import com.kefu.common.result.ResultGenerator;
import com.kefu.common.vo.DeleteVo;
import com.kefu.common.vo.DetailVo;
import com.kefu.common.vo.ImportResultVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Api(tags = "免打号码")
@RestController
@RequestMapping("call/avoid")
public class CallAvoidDialController extends AbstractBaseController {
    @Resource
    private CallAvoidDialService callAvoidDialService;

    @PostMapping("/add")
    @ApiOperation(value = "新增免打号码", notes = "新增免打号码")
    public Result<String> add(@Validated @RequestBody CallAvoidDialInsertVo callAvoidDialVo) {
        logger.info("新增免打号码:{}", callAvoidDialVo);

        CallAvoidDial info = callAvoidDialVo.convert();
        callAvoidDialService.save(info);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除免打号码", notes = "删除免打号码")
    public Result<String> delete(@Validated @RequestBody DeleteVo delete) {
        logger.info("删除免打号码:{}", delete);

        callAvoidDialService.deleteById(delete.getId());
        return ResultGenerator.genSuccessResult();
    }


    @PostMapping("/detail")
    @ApiOperation(value = "免打号码详情", notes = "免打号码详情")
    public Result<CallAvoidDial> detail(@Validated @RequestBody DetailVo detail) {
        logger.info("免打号码查看详情:{}", detail);

        CallAvoidDial callAvoidDial = callAvoidDialService.findById(detail.getId());
        return ResultGenerator.genSuccessResult(callAvoidDial);
    }

    @PostMapping("/list")
    @ApiOperation(value = "查询免打号码", notes = "查询免打号码")
    public Result<Page<CallAvoidDial>> list(@Validated @RequestBody CallAvoidDialSearchVo search) {
        logger.info("查询免打号码:{}", search);

        Page<CallAvoidDial> page = callAvoidDialService.searchLambdaPage(search);
        return ResultGenerator.genSuccessResult(page);
    }


    @PostMapping("/downloadImportExcel")
    @ApiOperation(value = "下载免打号码导入模板", notes = "下载免打号码导入模板")
    public void downloadImportExcel(HttpServletResponse response) throws IOException {
        List<Map<String, Object>> mapList = new ArrayList<>();
        Map<String, Object> map = Maps.newLinkedHashMap();
        map.put(msg("客户电话"), null);
        map.put(msg("客户姓名"), null);
        map.put(msg("备注"), null);
        mapList.add(map);
        excelWriter(mapList,response);
    }

    @PostMapping("/importAvoidDial")
    @ApiOperation(value = "导入免打号码", notes = "导入免打号码,最多导入1000条")
    public Result<ImportResultVo> importAvoidDial(MultipartFile file) throws IOException {
        List<Map<String, Object>> list = excelRead(file.getInputStream(),1000);
        List<CallAvoidDialInsertVo> imports = list.stream()
                .map(map -> {
                    CallAvoidDialInsertVo insertVo = new CallAvoidDialInsertVo();
                    Object phone = map.get(msg("客户电话"));
                    Object name = map.get(msg("客户姓名"));
                    Object desp = map.get(msg("备注"));
                    insertVo.setTelephone(phone == null ? "" : phone.toString());
                    insertVo.setName(name == null ? "" : name.toString());
                    insertVo.setDesb(desp == null ? "" : desp.toString());
                    return insertVo;
                }).collect(Collectors.toList());
        logger.info("导入免打号码 预计导入{}条", imports.size());
        return ResultGenerator.genSuccessResult(callAvoidDialService.importAvoidDial(imports));
    }
}
